﻿using System;
using System.Linq;
using Pap.Data;
using Pap.Data.Abstraction.Utils;

namespace Pap.Services.Customer
{
    /// <summary>
    /// 
    /// </summary>
    public class LatePayments : CustomerValidatorBase
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository adminRepository;
        /// <summary>
        /// Initializes a new instance of the <see cref="LatePayments"/> class.
        /// </summary>
        /// <param name="_db">The _DB.</param>
        /// <param name="repo">The repo.</param>
        public LatePayments(IDatabase _db, IAdministrationRepository repo)
            : base(_db)
        {
            isError = false;
            canValidateByName = true;
            adminRepository = repo;
        }

        /// <summary>
        /// Validates the specified current committee ID.
        /// </summary>
        /// <param name="currentCommitteeID">The current committee ID.</param>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        public override bool Validate(Guid currentCommitteeID, Pap.Data.ICustomer customer)
        {
            int allowedDiscreteDelay = int.Parse(adminRepository.GetSettingValue(currentCommitteeID, SettingKeys.AllowedDiscreteDelay));
            int allowedConsecutiveDelay = int.Parse(adminRepository.GetSettingValue(currentCommitteeID, SettingKeys.AllowedConsecutiveDelay));

            var delays = from loan in _database.CustomerLoans
                         join loanHistory in _database.CustomerLoanHistories
                         on loan.LoanID equals loanHistory.LoanID
                         where loan.CustomerID == customer.CustomerID
                         select new { ConsecutiveDelay = loanHistory.LastConsecutiveDelay, DiscreteDelay = loanHistory.LastDiscreteDelay };
            int maxDiscreteDelay = delays.Max(d => d.DiscreteDelay) ?? 0;
            int maxConsecutiveDelay = delays.Max(d => d.ConsecutiveDelay) ?? 0;

            if (maxConsecutiveDelay > allowedConsecutiveDelay || maxDiscreteDelay > allowedDiscreteDelay)
                return false;
            return true;
        }

        /// <summary>
        /// Validates the specified current committee ID.
        /// </summary>
        /// <param name="currentCommitteeID">The current committee ID.</param>
        /// <param name="customerName">Name of the customer.</param>
        /// <returns></returns>
        public override bool Validate(Guid currentCommitteeID, string customerName)
        {
#warning See how to get the current CommitteeID here;
            int allowedDiscreteDelay = int.Parse(adminRepository.GetSettingValue(currentCommitteeID, SettingKeys.AllowedDiscreteDelay));
            int allowedConsecutiveDelay = int.Parse(adminRepository.GetSettingValue(currentCommitteeID, SettingKeys.AllowedConsecutiveDelay));

            var delays = from loanHistory in _database.CustomerLoanHistories
                         where loanHistory.CustomerName.Contains(customerName)
                         select new { ConsecutiveDelay = loanHistory.LastConsecutiveDelay, DiscreteDelay = loanHistory.LastDiscreteDelay };
            int maxDiscreteDelay = delays.Max(d => d.DiscreteDelay) ?? 0;
            int maxConsecutiveDelay = delays.Max(d => d.ConsecutiveDelay) ?? 0;

            if (maxConsecutiveDelay > allowedConsecutiveDelay || maxDiscreteDelay > allowedDiscreteDelay)
                return false;
            else
                return true;
        }
    }
}
